package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.DeliveryAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.DeliveryLProductsist;
import com.isunland.managesystem.entity.rDistributionMain;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyDateUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * 配送货管理列表
 *
 * @author wu
 */
public class DeliveryListFragment extends BaseListFragment {

  public static final String EXTRA_STARTTIME = "com.isunland.managesystem.ui.EXTRA_STARTTIME";
  public static final String EXTRA_ENDTIME = "com.isunland.managesystem.ui.EXTRA_ENDTIME";
  public static final String EXTRA_DATASTATUS = "com.isunland.managesystem.ui.EXTRA_DATASTATUS";
  public static final String EXTRA_ISPAY = "com.isunland.managesystem.ui.EXTRA_ISPAY";
  public static final int REQUEST_UPDATE = 2;
  private static final int REQUEST_ADD = 0;
  private static final int REQUEST_QUERY = 1;
  private DeliveryAdapter mAdapter;
  private String mStartTime; // 开始时间
  private String mEndTime;// 结束时间
  //private String mDataStatus;// 数据状态
  private String mIsPay;// 是否支付
  private ArrayList<rDistributionMain> mList;

  @Override public void initData() {
    super.initData();
    mList = new ArrayList<>();
    mAdapter = new DeliveryAdapter(mActivity, mList, this);
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    setTitleCustom(R.string.deliveryManage);
    setListAdapter(mAdapter);
    mListview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
      @Override
      public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        mAdapter.initPopMenu(view.findViewById(R.id.iv_more));
        return true;
      }
    });
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("memberCode", CurrentUser.newInstance(getActivity()).getMemberCode());
    params.put("distributionStaffId", CurrentUser.newInstance(getActivity()).getJobNumber());
    params.put("orderField", "REG_DATE");
    params.put("orderSeq", "desc");
    params.put("inDataStatus", "waitCheck,waitSign,checkPass");
    if (mIsPay != null) {
      params.put("ifPay", mIsPay);
    }

    if (mStartTime != null) {
      params.put("beginQueryregDate", mStartTime);
      params.put("endQueryregDate", mEndTime);
    } else {
      String start = MyDateUtil.getBeforeMonth();
      //noinspection deprecation
      String end = MyDateUtil.dateToString(new Date(), MyDateUtil.TYPE_DATE);
      params.put("beginQueryregDate", start);
      params.put("endQueryregDate", end);
    }
    return params;
  }

  @Override public String getUrl() {
    return ApiConst.URL_DELIVERY_LIST;
  }

  @Override public void resolveResponse(String json) {
    DeliveryLProductsist products = new Gson().fromJson(json, DeliveryLProductsist.class);
    ArrayList<rDistributionMain> detail = products.getRows();
    mList.clear();
    mList.addAll(detail);
    mAdapter.notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    if (position < l.getHeaderViewsCount()) return;
    rDistributionMain detail = mAdapter.getItem(position - l.getHeaderViewsCount());
    //mAdapter.initPopMenu(v.findViewById(R.id.iv_more), detail);
    Intent intent = new Intent(mActivity, DeliveryDetailActivity.class);
    intent.putExtra(DeliveryDetailFragment.EXTRA_VALUE, detail);
    startActivityForResult(intent, DeliveryListFragment.REQUEST_UPDATE);
    super.onListItemClick(l, v, position, id);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_query, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    Intent intent;
    switch (item.getItemId()) {
      /**
       * 响应返回按钮
       */
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;

      case R.id.menu_item_add:
        intent = new Intent(getActivity(), DeliveryAddActivity.class);
        startActivityForResult(intent, REQUEST_ADD);
        break;

      case R.id.menu_item_query:
        intent = new Intent(getActivity(), DeliveryListQueryActivity.class);
        intent.putExtra(DeliveryListQueryFragment.REQUEST_START_TIME, mStartTime);
        intent.putExtra(DeliveryListQueryFragment.REQUEST_END_TIME, mEndTime);
        //intent.putExtra(DeliveryListQueryFragment.REQUEST_DATA_STATUS, mDataStatus);
        intent.putExtra(DeliveryListQueryFragment.REQUEST_ISPAY, mIsPay);
        startActivityForResult(intent, REQUEST_QUERY);
        break;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_ADD) {
      volleyPost();
      return;
    }
    if (requestCode == REQUEST_QUERY && data != null) {
      mStartTime = data.getStringExtra(EXTRA_STARTTIME);
      mEndTime = data.getStringExtra(EXTRA_ENDTIME);
      //mDataStatus = data.getStringExtra(EXTRA_DATASTATUS);
      mIsPay = data.getStringExtra(EXTRA_ISPAY);
      LogUtil.i("mStartTime==" + mStartTime + "mEndTime==" + mEndTime + "mIsPay==" + mIsPay);
      volleyPost();
    }

    if (requestCode == REQUEST_UPDATE) {
      volleyPost();
      return;
    }
    super.onActivityResult(requestCode, resultCode, data);
  }
}
